package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.entity.Question;
import com.lanou.service.QuestionService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName QuestionController
 * @Description TODO
 * @Author chenzhaobao
 * @Date 2025年08月14日 11:22
 * @Version 1.0
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @PostMapping
    public R insert(@RequestBody Question question) {
        int i = questionService.insert(question);
        if (i > 0) {
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id) {
        int i = questionService.deleteById(id);
        if (i > 0) {
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping
    public R updateById(@RequestBody Question question) {
        int i = questionService.updateById(question);
        if (i > 0) {
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    @GetMapping("/{id}")
    public R selectById(@PathVariable Integer id) {
        Question question = questionService.selectById(id);
        if (question != null) {
            return R.ok(question);
        }else {
            return R.fail("查询失败");
        }
    }
    @GetMapping
    public R selectAll() {
        List<Question> questionList = questionService.selectAll();
        if (questionList != null && questionList.size() > 0) {
            return R.ok(questionList);
        }else {
            return R.fail("查询失败");
        }
    }
    @GetMapping("/user/{userId}")
    public R selectByUserId(@PathVariable Integer userId) {
        List<Question> questionList = questionService.selectByUserId(userId);
        if (questionList != null && questionList.size() > 0) {
            return R.ok(questionList);
        }else {
            return R.fail("查询失败");
        }
    }

    @GetMapping("qa/{id}")
    public R getConsultantQA(@PathVariable("id") Integer consultantId,
                             @RequestParam(defaultValue = "1",required = false) Integer pageNum,
                             @RequestParam(defaultValue = "10",required = false) Integer pageSize) {
        PageInfo<Question> pageInfo = questionService.selectjl(consultantId, pageNum, pageSize);
        return R.ok(pageInfo);
    }
}
